import ex from './exportAndDownLoad'
const UpLoad = {
    data() {
        return {
            fileList: [],
            showId: '',
            visible: false,
            isEditName: false,
            formEdit: {
                fileName: ''
            },
            editRow: {},
            isImg: true
        }
    },
    mounted() {
        this.$fileApi = this.$Api.get(['common:upload'])
    },
    methods: {
        beforeUpload(response) {
            this.$fileApi.fileUpload(response, (res) => {
                if (res.code == 200) {
                    this.setFile(res.data)
                }
            })
            return false
        },
        resetData() {
            this.fileList = []
        },
        setFile(_array) {
            for (let i = 0; i < _array.length; i++) {
                const element = _array[i];
                this.fileList.push(element)
            }
        },
        getFile() {
            return this.fileList;
        },
        getFileById() {
            return this.fileList.map(obj => { return obj.id }).join(',')
        },
        getFileByArray() {
            return this.fileList.map(obj => { return obj.id })
        },
        renderTable() {
            this.$api[this.carry]({}, (res) => {
                this.fileList = res.data
            })
        },
        rename(row) {
            this.isEditName = true;
            this.editRow = row;
            this.formEdit.fileName = row.resourceName
        },
        saveName() {
            let sendObj = {
                id: this.editRow.id,
                filePath: this.editRow.filePath,
                fileSize: this.editRow.fileSize,
                resourceName: this.formEdit.fileName,
                ext: this.editRow.ext
            }
            for (let i = 0; i < this.fileList.length; i++) {
                const element = this.fileList[i];
                if (element.id == this.editRow.id) {
                    element.resourceName = this.formEdit.fileName
                }
            }
            this.$fileApi.fileUpdate(sendObj, res => {
                if (res.code == 200) {
                    this.$Message.success('保存成功！');
                    this.closeName()
                }
            })
        },
        closeName() {
            this.isEditName = false;
        },
        watchInfo(row) {
            this.visible = true
            if (row.ext == 'pdf' || row.ext == 'PDF') {
                this.isImg = false
            }
            this.showUrl = window.config.httpService + 'zcgl-admin/annex/show?id=' + row.id
        },
        closeShow() {
            this.visible = false;
        },
        download(row) {
            this.$fileApi.fileDownload({ id: row.id })
        },
        downloadUrl(path, loadFile) {
            let url = window.config.fileServer + 'download/' + path
            ex.downloadAttachment({
                url: url,
                fileName: loadFile.name + '.' + loadFile.ext
            })
        },
        remove(row) {
            this.fileList.splice(this.fileList.findIndex(item => item.id == row.id), 1)
        },
        packDownload(row) {
            let ids = this.fileList.map(item => { return item.id }).join(',')
            this.$fileApi.fileDownloadPackage({ ids: ids })
        }
    }
}
export { UpLoad }